package com.wwdx.controller.rest;

import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.WwdxVipProductModel;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.utils.ConstantHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

/**
 * @author liguoliang
 * @date 2018/5/24 10:05
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品接口")
public class RestProductController extends BaseController{

    /**
     * 主题包列表
     * @param type {@link WwdxThemeModel#type}
     */
    @RequestMapping("/theme/list")
    @ApiOperation(value = "主题包列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "page",value = "分页参数",defaultValue = "1"),
            @ApiImplicitParam(name = "rows",value = "分页参数",defaultValue = "10"),
            @ApiImplicitParam(name = "type",value = "主题包类型：0主题包1书单列表",defaultValue = "0")
    })
    public ResultBean themelist(String token, String sign,
                                @RequestParam(value = "type",defaultValue = "0") Integer type,
                                @RequestParam(value = "page",defaultValue = "1") Integer page,
                                @RequestParam(value = "rows",defaultValue = "10") Integer rows){

        ResultBean resultBean = super.validSignResult(super.getSortedMap(token,sign));
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String unionid = wwdxPersonalService.getByToken(token);
        WwdxThemeModel model = WwdxThemeModel.builder().status(0).type(type).build();
        Map<String,Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("count",wwdxThemeService.selectModelCount(model));
        map.put("list",productService.themeList(wwdxThemeService.selectModelList(model,page,rows),unionid));
        return renderSuccess(map);
    }

    /**
     * 主题包详情
     * @param themeid 主题包ID
     */
    @RequestMapping("/theme/detail")
    @ApiOperation(value = "主题包详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "themeid",value = "主题包ID",required = true)
    })
    public ResultBean themedetail(String token, String sign, Long themeid) {
        logger.info("主题包详情:{}",themeid);
        SortedMap<String, Object> sortedMap = super.getSortedMap(token,sign);
        sortedMap.put("themeid", themeid);
        ResultBean resultBean = super.validSignResult(sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String unionId = wwdxPersonalService.getByToken(token);
        return renderSuccess(productService.themeDetail(wwdxThemeService.getByThemeId(themeid), unionId));
    }

    /**
     * 书籍列表
     * @param unionid 用户业务ID
     * @param type {@link WwdxBookModel#type1}
     * @param type1 {@link WwdxBookModel#type}
     */
    @RequestMapping("/book/list")
    @ApiOperation(value = "书籍列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID"),
            @ApiImplicitParam(name = "type",value = "1 最新 2 最热",defaultValue = "1"),
            @ApiImplicitParam(name = "type1",value = "书籍类型 0 书籍 1 小课",defaultValue = "0"),
            @ApiImplicitParam(name = "page",value = "分页参数",defaultValue = "1"),
            @ApiImplicitParam(name = "rows",value = "分页参数",defaultValue = "10")
    })
    public ResultBean booklist(String unionid,Integer type,
                               @RequestParam(value = "type1",defaultValue = "0") int type1,
                               @RequestParam(value = "page",defaultValue = "1") int page,
                               @RequestParam(value = "rows",defaultValue = "10") int rows){

        WwdxBookModel wwdxBookModel = WwdxBookModel.builder().type(type1).type1(type).status(0).build();
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("book_list", productService.bookList(wwdxBookModel,unionid,page,rows));
        map.put("count",wwdxBookService.selectModelCount(wwdxBookModel));
        return renderSuccess(map);
    }

    /**
     * 书籍详情
     * @param bookid 书籍ID
     */
    @RequestMapping("/book/detail")
    @ApiOperation(value = "书籍详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "bookid",value = "书籍ID",required = true)
    })
    public ResultBean bookdetail(String token, String sign, Long bookid){
        logger.info("书籍详情:{}",bookid);
        SortedMap<String, Object> sortedMap = super.getSortedMap(token,sign);
        sortedMap.put("bookid",bookid);
        ResultBean resultBean = super.validSignResult(sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        String unionid = wwdxPersonalService.getByToken(token);
        return renderSuccess(productService.bookDetail(bookid, unionid));
    }

    /**
     * 首页热搜
     */
    @RequestMapping("/book/hotsearch")
    @ApiOperation(value = "首页热搜",httpMethod = "GET")
    public ResultBean bookhotsearch(){
        WwdxBookModel wwdxBookModel = WwdxBookModel.builder().status(0).type(0).random(true).build();
        return renderSuccess(wwdxBookService.selectModelList(wwdxBookModel,1,12));
    }

    /**
     * 首页书籍搜索
     * @param unionid 用户业务ID
     * @param word 搜索关键字
     */
    @RequestMapping("/book/search")
    @ApiOperation(value = "首页书籍搜索",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID"),
            @ApiImplicitParam(name = "word",value = "搜索关键字",required = true),
            @ApiImplicitParam(name = "page",value = "分页参数",defaultValue = "1"),
            @ApiImplicitParam(name = "rows",value = "分页参数",defaultValue = "10")
    })
    public ResultBean booksearch(String unionid, String word,
                                 @RequestParam(value = "page",defaultValue = "1") Integer page,
                                 @RequestParam(value = "rows",defaultValue = "10") Integer rows){
        logger.info("书籍或书籍作者搜索:{}",word);
        WwdxBookModel model = WwdxBookModel.builder().status(0).type(0).word(word).build();
        return renderSuccess(productService.bookList(wwdxBookService.selectModelList(model,page,rows),unionid));
    }

    /**
     * 书籍下章节列表
     * @param bookid 书籍ID
     * @param unionid 用户业务ID
     */
    @RequestMapping("/bookchapter/list")
    @ApiOperation(value = "书籍下章节列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID"),
            @ApiImplicitParam(name = "bookid",value = "书籍ID",required = true)
    })
    public ResultBean bookchapterlist(String unionid, Long bookid){
        return renderSuccess(wwdxBookChapterService.findBookChapterList(bookid,unionid));
    }

    /**
     * VIP产品列表 - h5接口
     */
    @RequestMapping("/vip/list2")
    @ApiOperation(value = "VIP产品列表 - 活动接口",httpMethod = "GET")
    public ResultBean viplist2(String unionid){
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        WwdxVipProductModel model = WwdxVipProductModel.builder().status(1).build();
        List<WwdxVipProductModel> wwdxVipProductModels = wwdxVipProductService.selectModelList(model, 1, 10);
        //查看是否有红包
        Boolean usable = false;
        Boolean aBoolean = hashOperations.hasKey(ConstantHolder.USER_RED_PACKET_COUNT, unionid);
        if (aBoolean){
            if ("1".equals(hashOperations.get(ConstantHolder.USER_RED_PACKET_COUNT,unionid))){
                for (WwdxVipProductModel wwdxVipProductModel : wwdxVipProductModels) {
                    wwdxVipProductModel.setPrice(wwdxVipProductModel.getPrice().subtract(new BigDecimal(35)));
                }
                usable = true;
            }else {usable = false;}
        }
        map.put("isRedPacket",usable);
        map.put("vipList",wwdxVipProductModels);
        return renderSuccess(map);
    }

    /**
     * VIP产品列表
     */
    @RequestMapping("/vip/list")
    @ApiOperation(value = "VIP产品列表",httpMethod = "GET")
    public ResultBean viplist(){
        WwdxVipProductModel model = WwdxVipProductModel.builder().status(1).build();
        List<WwdxVipProductModel> list2 = new ArrayList();
        for (WwdxVipProductModel wwdxVipProductModel : wwdxVipProductService.selectModelList(model, 1, 10)) {
            if (!"10006".equals(String.valueOf(wwdxVipProductModel.getProduct_id()))){
                list2.add(wwdxVipProductModel);
            }
        }
        return renderSuccess(list2);
    }

    /**
     * 用户VIP信息
     * @param unionid 用户业务ID
     */
    @RequestMapping("/vip/get")
    @ApiOperation(value = "用户VIP信息",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unionid",value = "用户业务ID",required = true)
    })
    public ResultBean vipget(String unionid){
        return renderSuccess(wwdxVipUserService.getByUnionId(unionid));
    }
}